package com.jsu.clubmanagementsystem.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jsu.clubmanagementsystem.common.BaseResponse;
import com.jsu.clubmanagementsystem.common.ErrorCode;
import com.jsu.clubmanagementsystem.common.PageQueryRequest;
import com.jsu.clubmanagementsystem.common.ResultUtils;
import com.jsu.clubmanagementsystem.exception.BusinessException;
import com.jsu.clubmanagementsystem.model.entity.SystemAnnouncement;
import com.jsu.clubmanagementsystem.service.system.SystemAnnouncementService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 社团公告操作
 */
@RestController
@Slf4j
@RequestMapping("/system")
public class SystemAnnouncementController {
    @Resource
    private SystemAnnouncementService systemAnnouncementService;

    @PostMapping("/gets")
    public BaseResponse<Page<SystemAnnouncement>> getSystemAnnouncements(@RequestBody PageQueryRequest page){
        return ResultUtils.success(systemAnnouncementService.getAll(page));
    }

    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deletes(@RequestParam("ids") List<Long> ids){
        systemAnnouncementService.deleteAll(ids);
        return ResultUtils.success(true);
    }

    @PostMapping("/add")
    public BaseResponse<SystemAnnouncement> add(@RequestBody SystemAnnouncement systemAnnouncement){
        log.error("add{}",systemAnnouncement);
        systemAnnouncementService.save(systemAnnouncement);
        return ResultUtils.success(systemAnnouncement);
    }

    @PostMapping("/update")
    public BaseResponse<SystemAnnouncement> update(@RequestBody SystemAnnouncement systemAnnouncement){
        if(systemAnnouncementService.getById(systemAnnouncement.getId())==null){
            throw new BusinessException(ErrorCode.NOT_FOUND,"未找到对应的公告");
        }
        systemAnnouncementService.updateById(systemAnnouncement);
        return ResultUtils.success(systemAnnouncement);
    }
}
